#include "ethernettestdialog.h"
#include "ui_ethernettestdialog.h"
#include "sourcemanage.h"

EthernetTestDialog::EthernetTestDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EthernetTestDialog)
{
    ui->setupUi(this);

    pNetDealwith=SM->GetNetDealwith();

    pNetSendReceive=SM->GetNetSendReceive();
    connect(pNetSendReceive,SIGNAL(SIG_ReceiveData(NetDataPacket)),this,SLOT(OnReceiveData(NetDataPacket)));

    connect(ui->pushButton_Cancel,SIGNAL(clicked()),this,SLOT(close()));
}

EthernetTestDialog::~EthernetTestDialog()
{
    delete ui;
}

void EthernetTestDialog::OnReceiveData(NetDataPacket recvPacket)
{
    QString text;
    for(int i=0;i<recvPacket.data.count();i++)
    {
        text.append(QString::number(recvPacket.data.at(i),16));
        text.append(" ");
    }
    ui->textEdit_Receive->setText(text);
}

void EthernetTestDialog::on_pushButton_Send_clicked()
{
    bool ok;
    bool state;
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.data.resize(0);
    receiveData.data.resize(0);

    ui->textEdit_Receive->clear();
    QString str=ui->textEdit_Send->toPlainText();
    QStringList strs=str.split(" ",QString::SkipEmptyParts);
    for(int i=0;i<strs.count();i++)
    {
        sendData.data[i]=strs[i].toLong(&ok,16);
    }

    switch(ui->comboBox->currentIndex())
    {
    case 0://2055
        sendData.cmd1=0x20;
        sendData.cmd2=0x55;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 1://2066
        sendData.cmd1=0x20;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 2://2155
        sendData.cmd1=0x21;
        sendData.cmd2=0x55;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 3://2455
        sendData.cmd1=0x24;
        sendData.cmd2=0x55;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 4://2466
        sendData.cmd1=0x24;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 5://2467
        sendData.cmd1=0x24;
        sendData.cmd2=0x67;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 6://2555
        sendData.cmd1=0x25;
        sendData.cmd2=0x55;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 7://2566
        sendData.cmd1=0x25;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 8://2655
        sendData.cmd1=0x26;
        sendData.cmd2=0x55;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 9://2666
        sendData.cmd1=0x26;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 10://2755
        sendData.cmd1=0x27;
        sendData.cmd2=0x55;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 11://2766
        sendData.cmd1=0x27;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 12://3166
        sendData.cmd1=0x31;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 13://3266
        sendData.cmd1=0x32;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 14://3366
        sendData.cmd1=0x33;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 15://3466
        sendData.cmd1=0x34;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 16://4066
        sendData.cmd1=0x40;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 17://4166
        sendData.cmd1=0x41;
        sendData.cmd2=0x66;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 18://4167
        sendData.cmd1=0x41;
        sendData.cmd2=0x67;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    case 19://4255
        sendData.cmd1=0x42;
        sendData.cmd2=0x55;
        sendData.length=sendData.data.count();
        state=pNetSendReceive->SendData(sendData,receiveData);
        break;
    default:
        return;
        break;
    }
}
